#include "iceframeworkengine.h"

//had to put this somewhere that gets compiled for everything
void dummyprintf(...) {}

ICEFrameworkEngine::ICEFrameworkEngine(void) : B_GameStateHandler()
{
	m_time = 0;
	m_device = NULL;
	m_CurrState = NULL;
	m_EventHandler = NULL;

	m_windowCaption[0] = '\0';

}

ICEFrameworkEngine::~ICEFrameworkEngine(void)
{
	if( m_CurrState ) delete m_CurrState;
	if( m_EventHandler ) delete m_EventHandler;

#if defined(ICE_ON_IRRLICHT)
	m_device->drop();
#endif

}

void* ICEFrameworkEngine::getAsset(char* assetName) {
	if( strcmp(assetName, "IRR_DEVICE") == 0 ) return (m_device);
	return NULL;
}

// called to start up the irrlicht engine and set up game variables
// E_DRIVER_TYPE == EDriverType in older versions
bool ICEFrameworkEngine::Init(const wchar_t *text, irr::video::E_DRIVER_TYPE device_type /*EDT_OPENGL*/,
								int res_x, int res_y, bool fullscreen /*false*/, int colordepth /*16*/,
								bool stencilbuffer /*false*/, bool restartDevice /*false*/)
{
	if(m_device) {
		ICEDBGMSG("ICEFrameworkEngine::Init() -- ERROR! m_device already initialized\n");
		return false;
	}

    //set up options
    if(!restartDevice) ICE_Options::Initialize();
	std::string res, device;


	if(ICE_Options::getConfig("configured") == "T" || restartDevice) { //load configured settings
		ICEDBGMSG("ICEFrameworkEngine::Init() -- loading configured settings from ICE_Options \n");
    	//find out whether to use fullscreen, and what color depth
    	fullscreen = ICE_Options::getConfig("fullscreen") == "T" ? true : false;
    	colordepth = ICE_Options::getConfig("colordepth") == "16" ? 16 : 32 ;

    	//get the resolution to use
    	res = ICE_Options::getConfig("resolution");
    	ICEDBGMSG("res: %s  on  ", res.c_str());
    	if(res == "640x480") {
    		res_x = 640; res_y = 480;
    	}else if( res == "800x600" ) {
    	    res_x = 800; res_y = 600;
        }else if( res == "1024x768" ) {
            res_x = 1024; res_y = 768;
        }
        //get the device type to use
        device = ICE_Options::getConfig("device");
        ICEDBGMSG("device: %s  \n", device.c_str());
        if(device == "OpenGL") {
            device_type = irr::video::EDT_OPENGL;
        }else if(device == "Direct3D8") {
            device_type = irr::video::EDT_DIRECT3D8;
        }else if(device == "Direct3D9") {
            device_type = irr::video::EDT_DIRECT3D9;
        }else if(device =="IrrlichtSoftware"){
            device_type = irr::video::EDT_SOFTWARE;
        }else if(device =="TheBurningsSoftwareRenderer"){
            device_type = irr::video::EDT_SOFTWARE2;
        }

    }else {
		ICEDBGMSG("ICEFrameworkEngine::Init() -- ICE_Options failed to load config, using default \n");
		fullscreen = false;
		colordepth = 16;
		res_x = 640; res_y = 480;
        //use defaults sent to function
    }
    //(init can be called while in game, in which case EH was already created)
    if(!m_EventHandler) m_EventHandler = new ICE_EventHandler(this);

	//set up IrrLicht
	//in older versions of irrlicht, remove the 'false' between stencil buffer and m_EventHandler
	m_device = irr::createDevice(device_type, irr::core::dimension2d<irr::s32>(res_x, res_y),
					colordepth, fullscreen, stencilbuffer, false, m_EventHandler);

	if(!m_device) {
		ICEDBGMSG("ICEFrameworkEngine::Init() -- ERROR! m_device failed creation\n");
		return false;
	}

	if(text) {
		wcscpy(m_windowCaption, text);
		m_device->setWindowCaption(text);
	}else {
		m_device->setWindowCaption(m_windowCaption); //set from memory (in case of restarting engine)
	}

    //if we havent been configured yet, go into config state
    //else start up directly into the first game state
    std::string configured = ICE_Options::getConfig("configured");
	if(configured == "F") {
		strcpy(m_CurrStateName, "ICE_VIDEO_CONFIG");
		m_CurrState = new gs_ICEVideoConfig(this, true);
	}

	return true;
}



//called in main loop to see if engine is still running
// returns false when the game is shutting down
bool ICEFrameworkEngine::Update()
{
    //ask the IrrLicht engine for another game loop
    if( !m_device->run() ) { return false; }

    //if RequestDestroy() was called, we'll call Destroy() now
    if( this->destroyMe ) { Destroy(); return false; }

    //update timer (though we dont use it right now)
    m_time = m_device->getTimer()->getTime();

    //update our state
	if(m_CurrState) m_CurrState->Update();

	return true;
}

bool ICEFrameworkEngine::RequestStateChange(char* newState) {
	strcpy(m_LastStateName, m_CurrStateName);
	if(m_CurrState) delete m_CurrState;
	m_CurrState = NULL;

	if( strcmp(newState, "ICE_VIDEO_CONFIG") == 0 ) {
		strcpy(m_CurrStateName, newState);
		m_CurrState = new gs_ICEVideoConfig(this);
		return true;
	}
	return false;
}

// event handler
bool ICEFrameworkEngine::OnEvent(irr::SEvent event)
{
    if(m_CurrState)
            return m_CurrState->onEvent(event);

	return false;
}

void ICEFrameworkEngine::Destroy() {
	//make sure we exit our current state
	if(m_CurrState) {
		delete m_CurrState;
		m_CurrState = NULL;
	}

	 //required to properly shut down IrrLicht
	m_device->closeDevice();
}

//******** EVENT HANDLER CLASS ********

ICE_EventHandler::ICE_EventHandler(ICEFrameworkEngine* engine) {
    m_pEngine = engine;
}
bool ICE_EventHandler::OnEvent(irr::SEvent event) {
    return m_pEngine->OnEvent(event);
}
